package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.Valid;

import cn.xshi.sys.model.SysMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysMessageService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
/**
 * @Desc 短消息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysMessage")
@Api(value = "短消息API",tags = "短消息API",description = "短消息API")
public class SysMessageController extends BaseAction {

	@Resource
	private SysMessageService sysMessageService;

	/**
	 * 查询短消息列表并分页
	 * @param baseSearch
	 */
	@PostMapping(value="/list")
	@Auth(value = "/sysMessage/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询短消息列表并分页", notes="查询短消息列表并分页")
	public BasePage<List<SysMessage>> getSysMessageListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		if("0".equals(condition.get("type"))){//我发送的消息
			condition.put("fromId", getCurrentAccountId());
		}else if("1".equals(condition.get("type"))){
			condition.put("toTd", getCurrentAccountId());
		}else{
			condition.put("toId", "-1");
		}
		commonHPager(baseSearch);
		List<SysMessage> sysMessageList = sysMessageService.getSysMessageListByCondition(condition);
		PageInfo<SysMessage> page = new PageInfo<SysMessage>(sysMessageList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	 * 查询单个短消息
	 * @param id
	 */
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysMessage/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询单个短消息", notes="查询单个短消息")
	public BaseResult<SysMessage> getSysMessageById(@PathVariable("id")String id, String type){
		if("1".equals(type)){
			Map<String, Object> condition = new HashMap<String,Object>();
			condition.put("isRead", 1);
			condition.put("readTime", getDate());
			condition.put("id", id);
			sysMessageService.updateRead(condition);
		}
		SysMessage sysMessage = sysMessageService.getSysMessageById(id);
		return outDataStr(sysMessage);
	}

	/**
	 * 添加
	 * @param sysMessage
	 * @param bindingResult
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个短消息", notes="创建单个短消息")
	public BaseResult addSysMessage(@Valid @RequestBody SysMessage sysMessage, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return outAudStr(false,backFem(bindingResult));
		}
		int i = 0;
		if(null != sysMessage){
			sysMessage.setId(toUUID());
			i= sysMessageService.addSysMessage(sysMessage);
		}
		if(i>0){
			return outAudStr(true,"发送成功");
		}else{
			return outAudStr(false,"发送失败");
		}
	}

	/**
	 * 修改
	 * @param sysMessage
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个短消息", notes="编辑单个短消息")
	public BaseResult updateSysMessage(@RequestBody SysMessage sysMessage){
		int i = 0;
		if(null != sysMessage){
			i= sysMessageService.updateSysMessage(sysMessage);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除短消息", notes="删除短消息")
	public BaseResult delSysMessage(String id,String type ){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			condition.put("type",type);
			i= sysMessageService.delSysMessage(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 更新已读状态
	 * @param sysMessage
	 * @return
	 */
	@PutMapping(value="/updateRead")
	@ApiOperation(value="更新已读状态", notes="更新已读状态")
	@Auth(value = "/sysMessage/updateRead",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult updateRead(@RequestBody SysMessage sysMessage){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("isRead", 1);
		condition.put("fromId", getCurrentAccountId());
		condition.put("toId", sysMessage.getToId());
		int i = sysMessageService.updateRead(condition);
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 加载历史列表数据并分页
	 * @param baseSearch
	 */
	@PostMapping(value="/his/list")
	@Auth(value = "/sysMessage/his/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="加载历史列表数据并分页", notes="加载历史列表数据并分页")
	public BasePage<List<SysMessage>> getSysMessageHisListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = new HashMap<String, Object>();
		commonHPager(baseSearch);
		condition.put("fromId", getCurrentAccountId());
		List<SysMessage> sysMessageList = sysMessageService.getSysMessageListByCondition(condition);
		PageInfo<SysMessage> page = new PageInfo<SysMessage>(sysMessageList);
		condition = new HashMap<String, Object>();
		condition.put("toTd", baseSearch.ConvertToMap().get("toId"));
		condition.put("fromId", getCurrentAccountId());
		condition.put("isRead", 1);
		sysMessageService.updateRead(condition);
		return outPageStr(page,baseSearch);
	}

	/**
	 * 获取未读消息个数
	 * @param sysMessage
	 * @return
	 */
	@PostMapping(value="/unRead/count")
	@Auth(value = "/sysMessage/unRead/count",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value="查询未读消息个数", notes="查询未读消息个数")
	public BaseResult<List<SysMessage>> getSysMessageUnReadCount(@RequestBody SysMessage sysMessage){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("toTd", getCurrentAccountId());
		condition.put("isRead", 0);
		List<SysMessage> sysMessageList = sysMessageService.getSysMessageCountByCondition(condition);
		return outItemsStr(sysMessageList);
	}
}
